<?php
/**
 * Created by PhpStorm.
 * User: yuhaoyu
 * Date: 12/22/18
 * Time: 1:26 PM
 */

namespace App\Repositories;


use App\Models\AccessInfo;
use App\Models\NowInfo;
use App\Models\UserInfo;

class UserRepository extends Repository
{
    protected $user;
    protected $now;
    protected $access;
    public function __construct(UserInfo $userInfo,NowInfo $now,AccessInfo $access){
        $this->model = $userInfo;
        $this->user = $userInfo;
        $this->now = $now;
        $this->access = $access;
    }

    public function add_user($uid,$name,$pswd){
        $this->user->name = $name;
        $this->user->username = $uid;
        $this->user->password = $pswd;
        $this->user->save();
    }
    public function scan($user)
    {
        $user_info = $this->get_by_id($user);
        if ($user_info == null)
            return "非本实验室人员";//非法进入
        $now_user = $this->now->where(['name' => $user_info->name])->first();
        if ($now_user == null){//不在实验室
            $this->now->name = $user_info->name;
            $this->now->time = time();
            $this->now->save();
            return $this->now->name."已进入";
        }
        //在实验室(出去)
        $name = $now_user->name;
        $this->access->name = $now_user->name;
        $this->access->in_time = $now_user->time;
        $this->access->out_time = time();
        $this->access->count = time() - $now_user->time;
        $now_user->delete();
        $this->access->save();
        return $name."已离开";
    }

    public function view()
    {
        return $this->model->select('id','name')->get();
    }

    public function access_view($type, $key)
    {
        date_default_timezone_set('PRC');
        $beginToday=mktime(0,0,0,date('m'),date('d'),date('Y'));
        if($type==0)
            return $this->access->where('name', 'like', '%'.$key.'%')->orderByDesc('out_time')->get()->toArray();
        if($type==1)
            return $this->access->where('name', 'like', '%'.$key.'%')->where('out_time','>',$beginToday)->orderByDesc('out_time')->get()->toArray();
        if($type==3){
            $before3day = mktime(0,0,0,date('m'),date('d')-3,date('Y'));
            return $this->access->where('name', 'like', '%'.$key.'%')->where('out_time','>',$before3day)->orderByDesc('out_time')->get()->toArray();
        }

    }

    public function now_view()
    {
        return $this->now->orderByDesc('time')->get()->toArray();
    }
    public function analysis_view($type,$key){
        date_default_timezone_set('PRC');
        $beginToday=mktime(0,0,0,date('m'),date('d'),date('Y'));
        $before3day = mktime(0,0,0,date('m'),date('d')-3,date('Y'));
        $stu_list = $this->user->where('name', 'like', '%'.$key.'%')->select('id','name','username')->get()->toArray();
        foreach ($stu_list as &$stu){
            $sum_time = 0;
            if($type==0)
                $stu_access = $this->access->where(['name'=>$stu['name']])->get()->toArray();
            if($type==1)
                $stu_access = $this->access->where(['name'=>$stu['name']])->where('out_time','>',$beginToday)->get()->toArray();
            if($type==3)
                $stu_access = $this->access->where(['name'=>$stu['name']])->where('out_time','>',$before3day)->get()->toArray();
            foreach ($stu_access as $single){
                $sum_time += $single["count"];
            }
            $stu['count'] = $sum_time;

        }
        return $stu_list;
    }
}